package demo;



import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 74646
 * Date: 2022-02-12
 * Time: 9:56
 */
public class TestDemo1 {
    public static void main(String[] args) {
        String str = "name=zhangsan&age=20";
        //按 "&" 进行分割
        String[] strings = str.split("&");
        for (String ret : strings) {
            System.out.println(ret);

            //再按 "=" 进行分割
            String[] ss = ret.split("=");
            for (String tmp : ss) {
                System.out.println(tmp);
            }
        }
    }

    public static void main31(String[] args) {
        String str = "ababcabcd";
        //将字符串中的第一个"ab"替换为字符串"vn"
        String s = str.replaceFirst("ab","vn");
        System.out.println(s);
    }

    public static void main30(String[] args) {
        String str = "ababcabcd";
        //将字符串中的"ab"替换为字符串"vn"
        String s = str.replaceAll("ab","vn");
        System.out.println(s);
    }

    public static void main29(String[] args) {
        String str = "ababcabcd";
        String tmp = "cd";
        //判断str指向的字符串是否以 "cd" 字符串结尾
        boolean flag = str.endsWith(tmp);
        System.out.println(flag);
    }

    public static void main28(String[] args) {
        String str = "ababcabcd";
        String tmp = "c";
        //从下标为4的位置开始，判断str指向的字符串是否以 "c" 字符串开头
        boolean flag = str.startsWith(tmp,4);
        System.out.println(flag);
    }

    public static void main27(String[] args) {
        String str = "ababcabcd";
        String tmp = "abc";
        //从下标为4的位置开始从后向前查找子串的起始位置
        int index = str.lastIndexOf(tmp,4);
        System.out.println(index);
    }

    public static void main26(String[] args) {
        String str = "ababcabcd";
        String tmp = "abc";
        //从下标为3的位置开始查找子串的起始位置
        int index = str.indexOf(tmp,3);
        System.out.println(index);
    }

    public static void main25(String[] args) {
        String str = "ababcabcd";
        String tmp = "abc";
        //返回主串中第一次出现子串的起始位置
        int index = str.indexOf(tmp);
        System.out.println(index);
        //boolean flag =  str.contains(tmp);
    }

    public static void main24(String[] args) {
        String str1 = "abc";
        String str2 = "Abc";
        int ret = str1.compareTo(str2);
        System.out.println(ret);
        System.out.println(str1.equalsIgnoreCase(str2));
    }

    public static void main23(String[] args) throws UnsupportedEncodingException {
        String str = "001";
        byte[] bytes = str.getBytes("utf-8");
        System.out.println(Arrays.toString(bytes));
        byte[] bytes2 = str.getBytes("GBK");
        System.out.println(Arrays.toString(bytes2));
        byte[] bytes3 = str.getBytes("ISO-8859-1");
        System.out.println(Arrays.toString(bytes3));
    }

    public static void main22(String[] args) {
        byte[] bytes = {97,98,99,100};
        String str = new String(bytes);
        System.out.println(str);
    }


    public static boolean isNumberChar(String s){
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
           boolean flag = Character.isDigit(ch);
           if(flag==false){
               return false;
           }
        }
        return true;
    }
    public static void main21(String[] args) {
        String str = "123a56";
        System.out.println(isNumberChar1(str));
    }

    public static boolean isNumberChar1(String s){
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if(ch<'0' || ch>'9'){
                return false;
            }
        }
        return true;
    }

    public static void main20(String[] args) {
        String str = "hello";
        char chars[] = str.toCharArray(); //将str指向的字符串对象，变成字符数组
        System.out.println(Arrays.toString(chars));
    }

    public static void main19(String[] args) {
        String str = "hello";
        char ch = str.charAt(2);//获取2下标的字符
        System.out.println(ch);
    }

    public static void main18(String[] args) {
        char ch[] = {'a','b','c','d','e'};
        String str = new String(ch,1,3); //从下标1开始，将后面的3个字符变成字符串
        System.out.println(str);
    }

    public static void main17(String[] args) {
        char ch[] ={'a','b','c','d','e'};
        String str = new String(ch);
        System.out.println(str);
    }


    public static void main16(String[] args) {
        String s1="abc"+"def";//1
        String s2=new String(s1);//2
        if(s1.equals(s2))//3
            System.out.println(".equals succeeded");//4
        if(s1==s2)//5
            System.out.println("==succeeded");//6
    }

    public static boolean isAdmin(String userId){
        return userId.toLowerCase()=="admin";
    }
    public static void main15(String[] args){
        boolean flag = isAdmin("Admin");
        System.out.println(flag);
    }

    public static void main14(String[] args) throws NoSuchFieldException, IllegalAccessException {
        String str = "Hello";
        // 获取 String 类中的 value 字段. 这个 value 和 String 源码中的 value 是匹配的.
        Field valueField = String.class.getDeclaredField("value");
        // 将这个字段的访问属性设为 true
        valueField.setAccessible(true);
        // 把 str 中的 value 属性获取到.
        char[] value = (char[]) valueField.get(str);
        // 修改 value 的值
        value[0] = 'h';
        System.out.println(str);
    }

    public static void main13(String[] args) {
        String str = "Hello";
        str = "h" + str.substring(1);
        System.out.println(str);
    }

    public static void main12(String[] args) {
        String str = "hello" ;
        str = str + " world" ;
        str += "!!!" ;
        System.out.println(str);
    }

    public static void main11(String[] args) {
        String str1 = null; //str1这个引用不指向任何对象
        String str2 = " "; //str2这个引用指向的字符串是空
        //这两个对象不可能相等
    }

    public static void main10(String[] args) {
        String str1 = "hello";
        System.out.println("hello".equals(str1));
    }

    public static void main9(String[] args) {
        String str1 = "hello1";
        String str2 = new String("hello");
        System.out.println(str1.equals(str2));
    }

    public static void main8(String[] args) {
        String str1 = "11";
        str1.intern();//手动入池
        String str2 = new String("1")+new String("1");
        System.out.println(str1==str2);
    }

    public static void main7(String[] args) {
        String str2 = new String("1")+new String("1");
        str2.intern();//手动入池
        String str1 = "11";
        System.out.println(str1==str2);
    }
    public static void main6(String[] args) {

        String str1 = "11";
        String str2 = new String("1")+new String("1");

        System.out.println(str1==str2);

    }

    public static void main5(String[] args) {
        String str1 = "hello";
        String str2 = "he";
        String str3 = str2 + "llo"; //str3 是一个变量，在编译的时候并不确定里面是什么
        System.out.println(str1 == str3);
    }

    public static void main4(String[] args) {
        String str1 = "hello";
        String str2 = "he"+"llo"; //此时，两个都是常量，在编译的时候就已经确定是"hello"
        System.out.println(str1==str2);
    }

    public static void main3(String[] args) {
        String str1 = "hello";
        String str2 = "hello";
        System.out.println(str1==str2);
    }

    public static void main2(String[] args) {

        String str1 = "hello";
        String str2 = new String("hello");
        System.out.println(str1==str2);

    }
    public static void main1(String[] args) {
        String str = "Hello";
        char[] chars = {'W','o','r','l','d'};
        func(str,chars);
        System.out.println(str);
        System.out.println(Arrays.toString(chars));
    }
    public static void func(String s,char[] array){
        s = "abcdef";
        array[0] = 'P';
    }
}
